import { Space, Tag } from 'antd'
import { getUnRepeatRow } from '../../../utils/array'

const getComplexValue = (value, key) => {
  if (value && value !== 'null') {
    const _value = typeof value === 'string' ? JSON.parse(value) : value
    if (key) {
      if (_value[key]) {
        return _value[key]
      } else {
        return []
      }
    } else {
      return _value
    }
  } else {
    return []
  }
}

export const complexObj = {
  'oryx-formreference': {
    type: 'formPicker'
  },
  'oryx-formproperties': {
    type: 'multipleComplex',
    inputProps: {
      title: '表单动态属性',
      getter: (value) => {
        return getComplexValue(value, 'formProperties')
      },
      setter: (newValue) => {
        return {
          formProperties: newValue
        }
      },
      createNewItem: (arr) => {
        return {
          ...getUnRepeatRow(arr, 'id', 'new_property_'),
          name: '',
          type: 'string',
          readable: true,
          writable: true
        }
      },
      schema: [
        {
          name: 'id',
          label: 'id',
          tableColumn: {}
        },
        {
          name: 'name',
          label: '名称',
          tableColumn: {
            width: 80
          },
        },
        {
          name: 'type',
          label: '类型',
          type: 'select',
          tableColumn: {
            width: 80
          },
          inputProps: {
            options: ['string', 'long', 'boolean', 'date', 'enum'].map(el => ({
              value: el,
              label: el
            }))
          }
        },
        {
          name: 'default',
          label: '默认值'
        },
        {
          name: 'expression',
          label: '表达式',
        },
        {
          name: 'variable',
          label: '变量',
        },
        {
          name: 'readable',
          label: '可读的',
          type: 'boolean',
        },
        {
          name: 'writable',
          label: '可写的',
          type: 'boolean',
        },
        {
          name: 'required',
          label: '必须的',
          type: 'boolean',
        }
      ]
    }
  },
  'oryx-dataproperties': {
    type: 'multipleComplex',
    inputProps: {
      title: '数据对象',
      icon: 'data',
      createNewItem: (arr) => {
        return {
          ...getUnRepeatRow(arr, 'dataproperty_id', 'new_data_object_'),
          dataproperty_type: 'string'
        }
      },
      getter: (value) => {
        return getComplexValue(value, 'items')
      },
      setter: (newValue) => {
        return {
          items: newValue
        }
      },
      schema: [
        {
          name: 'dataproperty_id',
          label: 'id',
          tableColumn: {}
        },
        {
          name: 'dataproperty_name',
          label: '名称',
          tableColumn: {}
        },
        {
          name: 'dataproperty_type',
          label: '类型',
          type: 'select',
          inputProps: {
            options: ['string', 'boolean', 'datetime', 'double', 'int', 'long'].map(el => ({
              value: el,
              label: el
            }))
          },
          tableColumn: {}
        },
        {
          name: 'dataproperty_value',
          label: '默认值',
          tableColumn: {}
        }
      ]
    }
  },
  'oryx-messagedefinitions': {
    type: 'multipleComplex',
    inputProps: {
      title: '消息定义',
      icon: 'message',
      getter: (value) => {
        return getComplexValue(value)
      },
      schema: [
        {
          name: 'id',
          label: 'id',
          tableColumn: {}
        },
        {
          name: 'name',
          label: '名称',
          tableColumn: {}
        }
      ]
    }
  },
  'oryx-escalationdefinitions': {
    type: 'multipleComplex',
    inputProps: {
      title: '升级的定义',
      icon: 'up',
      getter: (value) => {
        return getComplexValue(value)
      },
      schema: [
        {
          name: 'id',
          label: 'id',
          tableColumn: {}
        },
        {
          name: 'name',
          label: '名称',
          tableColumn: {}
        }
      ]
    }
  },
  'oryx-signaldefinitions': {
    type: 'multipleComplex',
    inputProps: {
      title: '信号定义',
      icon: 'signal',
      createNewItem: () => {
        return {
          scope: 'global'
        }
      },
      getter: (value) => {
        return getComplexValue(value)
      },
      schema: [
        {
          name: 'id',
          label: 'id',
          tableColumn: {}
        },
        {
          name: 'name',
          label: '名称',
          tableColumn: {}
        },
        {
          name: 'scope',
          label: 'scope',
          type: 'select',
          inputProps: {
            options: [
              {
                value: 'global',
                label: '全局'
              },
              {
                value: 'processInstance',
                label: '流程实例'
              }
            ]
          },
          tableColumn: {
            width: 120
          }
        }
      ]
    }
  },
  'oryx-executionlisteners': {
    type: 'multipleComplex',
    inputProps: {
      title: '执行监听器',
      icon: 'eye',
      width: 1100,
      formWidth: 550,
      createNewItem: () => {
        return {
          event: 'start'
        }
      },
      getter: (value) => {
        return getComplexValue(value, 'executionListeners')
      },
      setter: (newValue) => {
        return {
          executionListeners: newValue
        }
      },
      schema: [
        {
          name: 'event',
          label: '事件',
          type: 'radio',
          inputProps: {
            options: [
              { value: 'start', label: 'start' },
              { value: 'end', label: 'end' },
              { value: 'take', label: 'take' },
            ]
          },
          tableColumn: {}
        },
        {
          name: 'className',
          label: '类',
          tableColumn: {
            title: '实现'
          }
        },
        {
          name: 'expression',
          label: '表达式',
        },
        {
          name: 'delegateExpression',
          label: '委托表单式',
        },
        {
          name: 'fields',
          label: '字段',
          type: 'array',
          inputProps: {
            getNewRowData: (value) => {
              return {
                ...getUnRepeatRow(value, 'name', 'fieldName_')
              }
            },
            schema: [
              { name: 'name', label: '名称', span: 12 },
              { name: 'stringValue', label: '字符串值', span: 12 },
              { name: 'expression', label: '表达式', span: 12 },
              { name: 'string', label: '字符串', span: 12 },
            ]
          },
          // tableColumn: {}
        },
      ]
    }
  },
  'oryx-eventlisteners': {
    type: 'multipleComplex',
    inputProps: {
      title: '事件监听器',
      icon: 'eye',
      width: 1100,
      formWidth: 450,
      formProps: {
        labelCol: { span: 9 },
        wrapperCol: { span: 14 }
      },
      getter: (value) => {
        return getComplexValue(value, 'eventListeners')
      },
      setter: (newValue) => {
        return {
          eventListeners: newValue
        }
      },
      createNewItem: () => {
        return {
          rethrowEvent: false
        }
      },
      onInnerChange: (curId, values, setArr) => {
        const key = Object.keys(values)[0]
        let _values = {}
        if (key === 'events') {
          _values['event'] = values[key].map(el => el.event).filter(el => !!el).join(',')
        } else if (key === 'className') {
          _values['implementation'] = values[key]
        }
        // else if (key === 'rethrowType') {
        //   _values['implementation'] = 'Rethrow as ' + values[key]
        // } 
        else if (key === 'errorcode') {
          _values['implementation'] = 'Rethrow as error ' + values[key]
        } else if (key === 'messagename') {
          _values['implementation'] = 'Rethrow as message ' + values[key]
        } else if (key === 'signalname') {
          _values['implementation'] = 'Rethrow as signal ' + values[key]
        }
        setArr((arr) => {
          return arr.map(el => {
            if (el.$$hashKey === curId) {
              return {
                ...el,
                ..._values
              }
            } else {
              return el
            }
          })
        })
      },
      schema: [
        {
          name: 'event',
          label: '事件',
          inputProps: {
            disabled: true
          },
          hideInForm: true,
          tableColumn: {
            width: 250,
            render: (text, rowData) => {
              return rowData.event ? <Space direction="vertical">
                {rowData.event.split(',').map(el => <Tag
                  key={el}
                  color="blue"
                  style={{
                    wordBreak: 'break-all',
                    whiteSpace: 'unset',
                    display: 'block'
                  }}
                >{el}</Tag>)}
              </Space> : ''
            }
          }
        },
        {
          name: 'implementation',
          label: '实现',
          inputProps: {
            disabled: true
          },
          hideInForm: true,
          tableColumn: {}
        },
        {
          name: 'events',
          label: '事件',
          type: 'array',
          inputProps: {
            schema: [
              {
                name: 'event',
                label: '事件',
                type: 'select',
                inputProps: {
                  options: ['ACTIVITY_CANCELLED',
                    'ACTIVITY_COMPENSATE',
                    'ACTIVITY_COMPLETED',
                    'ACTIVITY_ERROR_RECEIVED',
                    'ACTIVITY_MESSAGE_CANCELLED',
                    'ACTIVITY_MESSAGE_RECEIVED',
                    'ACTIVITY_MESSAGE_WAITING',
                    'ACTIVITY_SIGNALED',
                    'ACTIVITY_SIGNAL_WAITING',
                    'ACTIVITY_STARTED',
                    'CUSTOM',
                    'ENGINE_CLOSED',
                    'ENGINE_CREATED',
                    'ENTITY_ACTIVATED',
                    'ENTITY_CREATED',
                    'ENTITY_DELETED',
                    'ENTITY_INITIALIZED',
                    'ENTITY_SUSPENDED',
                    'ENTITY_UPDATED',
                    'HISTORIC_ACTIVITY_INSTANCE_CREATED',
                    'HISTORIC_ACTIVITY_INSTANCE_ENDED',
                    'HISTORIC_PROCESS_INSTANCE_CREATED',
                    'HISTORIC_PROCESS_INSTANCE_ENDED',
                    'JOB_CANCELED',
                    'JOB_EXECUTION_FAILURE',
                    'JOB_EXECUTION_SUCCESS',
                    'JOB_RESCHEDULED',
                    'JOB_RETRIES_DECREMENTED',
                    'MEMBERSHIP_CREATED',
                    'MEMBERSHIP_DELETED',
                    'MEMBERSHIPS_DELETED',
                    'MULTI_INSTANCE_ACTIVITY_CANCELLED',
                    'MULTI_INSTANCE_ACTIVITY_COMPLETED',
                    'MULTI_INSTANCE_ACTIVITY_COMPLETED_WITH_CONDITION',
                    'MULTI_INSTANCE_ACTIVITY_STARTED',
                    'PROCESS_CANCELLED',
                    'PROCESS_COMPLETED',
                    'PROCESS_COMPLETED_WITH_TERMINATE_END_EVENT',
                    'PROCESS_COMPLETED_WITH_ERROR_END_EVENT',
                    'PROCESS_CREATED',
                    'PROCESS_STARTED',
                    'SEQUENCEFLOW_TAKEN',
                    'TASK_ASSIGNED',
                    'TASK_COMPLETED',
                    'TASK_CREATED',
                    'TIMER_FIRED',
                    'TIMER_SCHEDULED',
                    'UNCAUGHT_BPMN_ERROR',
                    'VARIABLE_CREATED',
                    'VARIABLE_DELETED',
                    'VARIABLE_UPDATED'
                  ].map(el => ({
                    value: el,
                    label: el
                  }))
                }
              }
            ]
          }
        },
        {
          name: 'rethrowEvent',
          label: 'Rethrow Event',
          type: 'boolean'
        },
        {
          name: 'className',
          label: '类',
          hide: (form) => {
            return !form.getFieldValue('rethrowEvent')
          }
        },
        {
          name: 'delegateExpression',
          label: '委托表达式',
          hide: (form) => {
            return !form.getFieldValue('rethrowEvent')
          }
        },
        {
          name: 'entityType',
          label: '实体类型',
          hide: (form) => {
            return !form.getFieldValue('rethrowEvent')
          }
        },
        {
          name: 'rethrowType',
          label: 'RethrowEventType',
          type: 'select',
          inputProps: {
            options: ['error', 'message', 'signal', 'globalSignal'].map(el => ({
              value: el,
              label: el
            }))
          },
          hide: (form) => {
            return !!form.getFieldValue('rethrowEvent')
          }
        },
        {
          name: 'errorcode',
          label: '错误代码',
          hide: (form) => {
            return !!form.getFieldValue('rethrowEvent') && form.getFieldValue('rethrowType') === 'error'
          }
        },
        {
          name: 'messagename',
          label: '消息名称',
          hide: (form) => {
            return !!form.getFieldValue('rethrowEvent') && form.getFieldValue('rethrowType') === 'message'
          }
        },
        {
          name: 'signalname',
          label: '信号名称',
          hide: (form) => {
            return !!form.getFieldValue('rethrowEvent') && (
              form.getFieldValue('rethrowType') === 'signal' ||
              form.getFieldValue('rethrowType') === 'globalSignal'
            )
          }
        }
      ]
    }
  },
  'oryx-tasklisteners': {
    type: 'multipleComplex',
    inputProps: {
      title: '任务监听器',
      icon: 'eye',
      width: 1100,
      formWidth: 550,
      createNewItem: () => {
        return {
          event: 'create'
        }
      },
      getter: (value) => {
        return getComplexValue(value, 'taskListeners')
      },
      setter: (newValue) => {
        return {
          taskListeners: newValue
        }
      },
      schema: [
        {
          name: 'event',
          label: '事件',
          type: 'radio',
          inputProps: {
            options: [
              { value: 'create', label: 'create' },
              { value: 'assignment', label: 'assignment' },
              { value: 'complete', label: 'complete' },
              { value: 'delete', label: 'delete' },
            ]
          },
          tableColumn: {}
        },
        {
          name: 'className',
          label: '类',
          tableColumn: {
            title: '实现'
          }
        },
        {
          name: 'expression',
          label: '表达式',
        },
        {
          name: 'delegateExpression',
          label: '委托表单式',
        },
        {
          name: 'fields',
          label: '字段',
          type: 'array',
          inputProps: {
            getNewRowData: (value) => {
              return {
                ...getUnRepeatRow(value, 'name', 'fieldName_')
              }
            },
            schema: [
              { name: 'name', label: '名称' },
              { name: 'stringValue', label: '字符串值' },
              { name: 'expression', label: '表达式' },
              { name: 'string', label: '字符串' },
            ]
          },
          // tableColumn: {}
        },
      ]
    }
  },
  'oryx-usertaskassignment': {
    type: 'userTaskAssignment'
  }
}